import {createRouter, createWebHashHistory} from 'vue-router';
import Cookies from "js-cookie";


const routes = [
    {
        path: '/',
        component: () => import('@/layout/index.vue'),
        redirect: '/dashBoard',
        children: [
            // 系统管理路由：
            {
                path: '/dashBoard',
                name: 'DashBoard',
                component: () => import('@/views/dashBoard/index.vue'),
                meta: {title: 'DashBoard'} // 添加元信息
            },
            {
                path: '/sys/user',
                name: '用户管理',
                component: () => import('@/views/sys/user/index.vue')
            },
            {
                path: '/sys/role',
                name: '角色管理',
                component: () => import('@/views/sys/role/index.vue')
            },
            {
                path: '/sys/menu',
                name: '菜单管理',
                component: () => import('@/views/sys/menu/index.vue')
            },
            {
                path: '/userCenter',
                name: '个人中心',
                component: () => import('@/views/userCenter/index.vue')
            },
            {
                path: '/sys/logs',
                name: '登陆日志',
                component: () => import('@/views/sys/logs/index.vue')
            },
            // 系统功能路由
            {
                path: '/host',
                name: '主机管理',
                component: () => import('@/views/mgr/host/index.vue')
            },
            // terminal终端
            {
                path: '/terminal',
                name: 'terminal终端',
                component: () => import('@/views/mgr/terminal/index.vue')
            },
            // 命令批量执行
            {
                path: '/job',
                name: '批量执行',
                component: () => import('@/views/job/index.vue')
            },
            // 文件分发
            {
                path: '/file/distribution',
                name: '文件分发',
                component: () => import('@/views/fileDistribution/index.vue')
            },
            // 监控中心
            {
                path: '/monitor',
                name: '监控中心',
                component: () => import('@/views/monitorCenter/index.vue')
            },
            // 告警中心/告警记录
            {
                path: '/alert/logs',
                name: '告警发送记录',
                component: () => import('@/views/alertCenter/alertLogs/index.vue')
            },
            // 告警中心/告警联系人
            {
                path: '/alert/contactPerson',
                name: '告警联系人',
                component: () => import('@/views/alertCenter/alertContactPerson/index.vue')
            },
            // 告警中心/告警联系组
            {
                path: '/alert/contactGroup',
                name: '告警联系组',
                component: () => import('@/views/alertCenter/alertContactGroup/index.vue')
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue')
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

// 全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
    // 判断有没有登录
    if (!Cookies.get('token')) {
        if (to.path === "/login") {
            next();
        } else {
            router.replace('/login');
        }
    } else {
        next();
    }
});
export default router;
